package com.hs.srm.tools;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Properties;

/**
 * 邮箱工具
 * @date 2019-7-24
 */
public class MailUtils {

    //邮件服务器
    public static String host_163="smtp.163.com";
    //端口
    public static Integer port= 25;
    //用户名
    public static String userName = "";//"yf761423958";
    //密码
    public static String passWord= "";//"yanfei521";
    //发件人
    public static String emailForm= "";//"yf761423958@163.com";
    //时间延迟
    public static String timeout= "25000";

    /**
     *
     * @return 配置好的工具
     */
    private static JavaMailSenderImpl createMailSender() {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost(host_163);
        sender.setPort(port);
        sender.setUsername(userName);
        sender.setPassword(passWord);
        sender.setDefaultEncoding("Utf-8");
        Properties p = new Properties();
        p.setProperty("mail.smtp.timeout", timeout);
        //邮箱认证
        p.setProperty("mail.smtp.auth", "true");
        sender.setJavaMailProperties(p);
        return sender;
    }

    /**
     * 发送邮件
     *
     * @param to 接受人
     * @param subject 主题
     * @param content 发送内容
     * @throws MessagingException 异常
     * @throws UnsupportedEncodingException 异常
     */
    public static boolean sendMail(String to, String subject, String content) throws MessagingException, IOException {
       String[] tos=new String[1];
        tos[0]=to;
        return sendMail(tos,subject,content);
    }

    /**
     * 发送邮件
     *
     * @param to 接受人
     * @param subject 主题
     * @param content 发送内容
     * @throws MessagingException 异常
     * @throws UnsupportedEncodingException 异常
     */
    public static boolean sendMail(String[] to, String subject, String content) throws MessagingException, IOException {
        return sendMail(to,subject,content,null,null);
    }

    /**
     * 发送邮件
     *
     * @param to 接受人
     * @param subject 主题
     * @param content 发送内容
     * @throws MessagingException 异常
     * @throws UnsupportedEncodingException 异常
     */
    public static boolean sendMail(List<String> to, String subject, String content) throws MessagingException {
        String[] tos = new String[to.size()];
        to.toArray(tos);
        return sendMail(tos, subject, content,null,null);
    }

    /**
     * 发送邮件
     *
     * @param to 接受人
     * @param subject 主题
     * @param content 发送内容
     * @param fiuleName 附件名称
     * @param file 附件
     * @throws MessagingException 异常
     * @throws UnsupportedEncodingException 异常
     */
    public static boolean sendMail(List<String> to, String subject, String content, String fiuleName, File file) throws MessagingException {
        String[] tos = new String[to.size()];
        to.toArray(tos);
        return sendMail(tos, subject, content,fiuleName,file);
    }

    /**
     * 发送邮件
     *
     * @param to 接受人
     * @param subject 主题
     * @param content 发送内容
     * @param fiuleName 附件名称
     * @param file 附件
     * @throws MessagingException 异常
     * @throws UnsupportedEncodingException 异常
     */
    public static boolean sendMail(String[] to, String subject, String content,String fiuleName,File file) throws MessagingException {
        JavaMailSenderImpl mailSender=createMailSender();
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        // 设置utf-8或GBK编码，否则邮件会有乱码
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        messageHelper.setFrom(emailForm);
        messageHelper.setTo(to);
        messageHelper.setSubject(subject);
        messageHelper.setText(content);
        //附件
        if(fiuleName!=null&&!"".equals(fiuleName)){
            messageHelper.addAttachment(fiuleName, file);
        }
        mailSender.send(mimeMessage);
        return true;
    }
}
